package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.home.adapter.MyCollectionAndErrListAdapter;
import com.shushan.mistake.MistakeDetailActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.pages.bean.ExerciseRecordBean;
import com.shushan.pages.bean.TypesExerciseRecordBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.ToolUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: MyWrongTopicActivity
 * @Desciption: //我的错题
 * @author: zhangshihao
 * @date: 2018-04-11
 */
public class MyWrongTopicActivity extends NavigationLoseActivity {

    private static final String TAG = MyWrongTopicActivity.class.getSimpleName();

    private ImageView ivMidPic;

    private RecyclerView recyclerView;
    private RelativeLayout rlHeader;
    private LinearLayout llEmptyState;
    private TextView tvHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wrong);
        setTitle("我的错题");
        initView();
        getData();
    }

    private void initView() {
        ivMidPic = findViewById(R.id.iv_mid_pic);
        recyclerView = findViewById(R.id.lv_my_collection_list);
        rlHeader = findViewById(R.id.rl_header);
        llEmptyState = findViewById(R.id.ll_empty_state);
        llEmptyState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
        tvHint = findViewById(R.id.tv_hint);
        ivMidPic.setImageResource(R.drawable.ic_cry_face);
    }

    private void getData() {
        int child_id = BabyApplication.getInstance().getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.MY_ERROR_TOPIC_NEW);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id", child_id);
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<ExerciseRecordBean>() {

                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(ExerciseRecordBean errorExerciseRecordBean) {
                        errorExerciseRecordBean.typesExerciseRecordBeans.remove(1);//去掉智能练习数据
                        errorExerciseRecordBean.nums.remove(1);//去掉智能练习数据
                        if(ToolUtils.isNoData(errorExerciseRecordBean.nums)){
                            rlHeader.setVisibility(View.GONE);
                            recyclerView.setVisibility(View.GONE);
                            llEmptyState.setVisibility(View.VISIBLE);
                            tvHint.setText("暂无错题,可点击重试");
                        }else {
                            rlHeader.setVisibility(View.VISIBLE);
                            recyclerView.setVisibility(View.VISIBLE);
                            llEmptyState.setVisibility(View.GONE);
                            List<TypesExerciseRecordBean> recordBeans = errorExerciseRecordBean.typesExerciseRecordBeans;
                            List<TypesExerciseRecordBean> tempRecordBeans = new ArrayList<>();
                            tempRecordBeans.addAll(recordBeans);
                            List<HashMap<Integer,Integer>> nums = errorExerciseRecordBean.nums;
                            for(int i=0;i<nums.size();i++){
                                TypesExerciseRecordBean recordBean = tempRecordBeans.get(i);
                                int[] errNumArr = recordBean.err_num_of_subjects;
                                int count = 0;
                                for(int j=0;j<errNumArr.length;j++){
                                    count+=errNumArr[j];
                                }
                                if(count == 0){
                                    recordBeans.remove(recordBean);
                                }
                            }
                            MyCollectionAndErrListAdapter adapter = new MyCollectionAndErrListAdapter(MyWrongTopicActivity.this,
                                    recordBeans, nums);
                            adapter.setFromWrongTopic(true);
                            adapter.setOnItemClickListener(new MyCollectionAndErrListAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(int errNum,int subjectId, int exercisesType) {
                                    Intent intent = new Intent(MyWrongTopicActivity.this, MistakeDetailActivity.class);
                                    intent.putExtra("num", errNum);
                                    intent.putExtra("subjectId", subjectId);
                                    intent.putExtra("exercisesType", exercisesType);
                                    startActivityAllow(intent);
                                }
                            });
                            recyclerView.setLayoutManager(new LinearLayoutManager(MyWrongTopicActivity.this));
                            recyclerView.setAdapter(adapter);
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                        rlHeader.setVisibility(View.GONE);
                        recyclerView.setVisibility(View.GONE);
                        llEmptyState.setVisibility(View.VISIBLE);
                        tvHint.setText("当前网络缓慢，请点击重试");
                    }
                });

    }

}
